﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RelationExpertSystem.Models;
using System.Workflow.Activities.Rules;

namespace RelationExpertSystem.Controllers
{
    public class RelationController
    {
        RelationModel _model;

        public event EventHandler<FactsEventArgs> NewFactsLoaded
        {
            add { _model.NewFactsLoaded += value; }
            remove { _model.NewFactsLoaded -= value; }
        }

        public event EventHandler<FactsEventArgs> FactsUpdated
        {
            add { _model.FactsUpdated += value; }
            remove { _model.FactsUpdated -= value; }
        }

        public event EventHandler<MessageEventArgs> NewInference
        {
            add { _model.NewInference += value; }
            remove { _model.NewInference -= value; }
        }

        public event EventHandler RulesUpdated
        {
            add { _model.RulesUpdated += value; }
            remove { _model.RulesUpdated -= value; }
        }

        public RelationController()
        {
            RelationRulesManager rulesManager = new RelationRulesManager();
            //ISession session = new TestRelationSession(rulesManager);
            ISession session = new RelationSession(rulesManager);
            _model = new RelationModel(rulesManager, session);
        }

        //TODO: move all logic from RelationForm into this Controller

        public void Execute()
        {
            //RelationExpertSystem.Relation.DependencyPrinter.PrintDependencies();
            RelationshipForm form = new RelationshipForm(this);
            form.ShowDialog();
        }

        public void NewRules(string name, string description)
        {
            _model.NewRules(name, description);
        }

        internal void LoadRules()
        {
            _model.LoadRules();
        }

        public void EditRules()
        {
            _model.EditRules();
        }

        public IEnumerable<Rule> GetRules()
        {
            return _model.GetRules();
        }

        public void LoadFacts()
        {
            _model.LoadFacts();
        }

        public void EditFacts()
        {
            _model.EditFacts();
        }

        public void NewFacts()
        {
            _model.NewFacts();
        }

        public void Infer()
        {
            _model.Infer();
        }
    }
}
